﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using System.Collections;
using System.Data;
using ESRI.ArcGIS.Geodatabase;

namespace DIWAGIS.Class
{
    public class WorkingWithLayer
    {
        /// <summary>
        /// Xóa layer theo tên trên mapcontrol
        /// </summary>
        /// <param name="LayerName"></param>
        /// <param name="MapControl"></param>
        /// 
        /// Author    Date    Comments
        
        public static void DeleteLayerByName(string LayerName, IMapControl3 MapControl)
        {
            int i = 0;
            ILayer pLayer;
            IEnumLayer m_EnumLayer = MapControl.Map.get_Layers(null, true);
            m_EnumLayer.Reset();
            for (pLayer = m_EnumLayer.Next(); pLayer != null; pLayer = m_EnumLayer.Next())
            {
                //if ((pLayer is IFeatureLayer) || (pLayer is IGroupLayer))
                //{
                if (pLayer.Name == LayerName)
                {
                    MapControl.DeleteLayer(i);
                    break;
                }
                //}
                i++;
            }
            MapControl.ActiveView.Refresh();
        }
        /// <summary>
        /// Lấy layer theo tên trên mapcontrol
        /// </summary>
        /// <param name="LayerName"></param>
        /// <param name="MapControl"></param>
        /// <returns></returns>
        /// 
        /// Author    Date    Comments
        /// 
        public static ILayer GetLayerByName(string LayerName, IMapControl4 MapControl)
        {
            ILayer result = null;
            IEnumLayer m_EnumLayer;
            m_EnumLayer = MapControl.Map.get_Layers(null, true);
            if (m_EnumLayer != null)
            {
                ArrayList lstLayer = new ArrayList();
                ILayer pLayer;
                m_EnumLayer.Reset();
                for (pLayer = m_EnumLayer.Next(); pLayer != null; pLayer = m_EnumLayer.Next())
                {
                    if (pLayer is IFeatureLayer)
                    {
                        if (pLayer.Name == LayerName)
                            result = pLayer;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// trả về một list tất cả những layer trên mapcontrol
        /// </summary>
        /// <param name="MapControl"></param>
        /// <returns></returns>
        public static IList<ILayer> GetAllLayer(IMapControl3 MapControl)
        {
            IList<ILayer> result = new List<ILayer>();
            IEnumLayer m_EnumLayer;
            m_EnumLayer = MapControl.Map.get_Layers(null, true);
            if (m_EnumLayer != null)
            {
                ArrayList lstLayer = new ArrayList();
                ILayer pLayer;
                m_EnumLayer.Reset();
                for (pLayer = m_EnumLayer.Next(); pLayer != null; pLayer = m_EnumLayer.Next())
                {
                    if (pLayer is IFeatureLayer)
                    {
                        result.Add(pLayer);
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// trả về một list tất cả những layer trên mapcontrol
        /// </summary>
        /// <param name="MapControl"></param>
        /// <returns></returns>
        public static DataTable GetLayerNameAndAlias(IMapControl3 MapControl)
        {
            DataTable tblresult = new DataTable();
            DataColumn colName = new DataColumn("Name", typeof(string));
            DataColumn colAlias = new DataColumn("Alias", typeof(string));
            tblresult.Columns.Add(colName);
            tblresult.Columns.Add(colAlias);

            DataRow newRow = tblresult.NewRow();
            newRow["Name"] = string.Empty;
            newRow["Alias"] = string.Empty;
            tblresult.Rows.Add(newRow);

            IEnumLayer m_EnumLayer;
            m_EnumLayer = MapControl.Map.get_Layers(null, true);
            if (m_EnumLayer != null)
            {
                ArrayList lstLayer = new ArrayList();
                ILayer pLayer;
                m_EnumLayer.Reset();
                for (pLayer = m_EnumLayer.Next(); pLayer != null; pLayer = m_EnumLayer.Next())
                {
                    if (pLayer is IFeatureLayer)
                    {
                        newRow = tblresult.NewRow();
                        IDataset dset = ((IFeatureLayer)pLayer).FeatureClass as IDataset;
                        newRow["Name"] = dset.Name;
                        newRow["Alias"] = pLayer.Name;
                        tblresult.Rows.Add(newRow);
                    }
                }
            }
            return tblresult;
        }

        /// <summary>
        /// Lấy tên layer từ tên của featureclass
        /// </summary>
        /// <param name="MapControl"></param>
        /// <param name="strFeatureClassName"></param>
        /// <returns></returns>
        public static string GetLayerNameFromFeatureClassName(IMapControl4 MapControl, string strFeatureClassName)
        {
            string result = string.Empty;
            IEnumLayer m_EnumLayer;
            m_EnumLayer = MapControl.Map.get_Layers(null, true);
            if (m_EnumLayer != null)
            {
                ArrayList lstLayer = new ArrayList();
                ILayer pLayer;
                m_EnumLayer.Reset();
                for (pLayer = m_EnumLayer.Next(); pLayer != null; pLayer = m_EnumLayer.Next())
                {
                    if (pLayer is IFeatureLayer)
                    {
                        IFeatureClass fclass = ((IFeatureLayer)pLayer).FeatureClass;
                        IDataset dset = fclass as IDataset;
                        if (dset.Name == strFeatureClassName)
                        {
                            result = pLayer.Name;
                            break;
                        }
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// Lấy tất cả những layer đang hiển thị
        /// </summary>
        /// <param name="MapControl"></param>
        /// <returns></returns>
        public static IList<ILayer> GetAllVisibleLayer(IMapControl3 MapControl)
        {
            IList<ILayer> result = new List<ILayer>();
            IEnumLayer m_EnumLayer;
            m_EnumLayer = MapControl.Map.get_Layers(null, true);
            if (m_EnumLayer != null)
            {
                ArrayList lstLayer = new ArrayList();
                ILayer pLayer;
                m_EnumLayer.Reset();
                for (pLayer = m_EnumLayer.Next(); pLayer != null; pLayer = m_EnumLayer.Next())
                {
                    if ((pLayer is IFeatureLayer) && (pLayer.Visible == true))
                    {
                        result.Add(pLayer);
                    }
                }
            }
            return result;
        }
    }
}
